package com.treasure.mall.biz.entity.order;

import com.alibaba.fastjson.JSONObject;
import com.treasure.mall.biz.bo.SystemInfoData;
import com.treasure.mall.biz.entity.embeddable.ReceivingInfo;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;

/**
 * 订单
 *
 * @author allan
 * @version 1.0.0
 * @date 2020/6/11
 */
@Entity
@Table(name = "zb_order")
@Setter
@Getter
@Cacheable(false)
public class DealOrderPO extends OrderPO {
    /**
     * 收货信息
     */
    private ReceivingInfo receivingInfo;
    /**
     * 商品数量
     */
    @Column(name = "goods_num")
    private int goodsNum;
    /**
     * 商品金额
     */
    @Column(name = "goods_amount", scale = 2, precision = 10)
    private BigDecimal goodsAmount;
    /**
     * 运费
     */
    @Column(name = "freight_amount", scale = 2, precision = 9)
    private BigDecimal freightAmount;
    /**
     * 优惠金额
     */
    @Column(name = "pmt_amount", scale = 2, precision = 9)
    private BigDecimal pmtAmount;
    /**
     * 已经抵扣的钱（比如拍卖的保证金）
     */
    @Column(name = "deduct_amount", scale = 2, precision = 10)
    private BigDecimal deductAmount;
    /**
     * 鉴定服务费
     */
    private BigDecimal identifyFee;
    /**
     * 抵扣的余额
     */
    @Column(name = "balance", scale = 2, precision = 10)
    private BigDecimal balance;
    /**
     * 是否需要鉴定服务
     */
    @Column(name = "is_need_identify")
    private boolean needIdentify;

    @OneToMany(mappedBy = "orderPO", fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, orphanRemoval = true)
    private List<SubOrderPO> subOrderPOS;

    public boolean isReceived() {
        return subOrderPOS.stream().allMatch(SubOrderPO::isReceived);
    }

    /**
     * 额外信息
     */
    @Column(name = "extend_data", columnDefinition = "text")
    private String extendData;


    /**
     * 获取系统信息
     *
     * @return
     */
    public SystemInfoData getSystemInfo() {
        try {
            if (StringUtils.isEmpty(extendData)) {
                return new SystemInfoData();
            }
            return JSONObject.parseObject(extendData, SystemInfoData.class);
        } catch (Exception e) {
            return new SystemInfoData();
        }
    }
}
